var url = 'wss://crm.bxwhl.com/wss:2345';

function connect(user,func) {
  var that = this;
  wx.connectSocket({
    url: url,
    header:{'content-type': 'application/json'},
    success: function () {
      console.log('连接成功~')
    },
    fail: function () {
      console.log('连接失败~')
    } 
  })
  wx.onSocketOpen(function (res) {
    console.log(res)
    wx.sendSocketMessage({ 
      data: JSON.stringify({sendid : user}) 
    });
     //接受服务器消息
    wx.onSocketMessage(func);//func回调可以拿到服务器返回的数据
  });

  wx.onSocketError(function (res) {
    console.log(res)
    wx.showToast({
      title: '连接失败，请检查！',
      icon: "none",
      duration: 2000
    })
    wx.connectSocket({
      url: url,
    })
  })
}
//发送消息
function send(msg) {
  wx.sendSocketMessage({ 
    data: msg 
  });
}
module.exports = {
  connect: connect,
  send: send
}